package com.aries.ui.widget.alert;


import com.aries.ui.widget.BasisDialog;
import com.aries.ui.util.DrawableUtil;
import com.aries.ui.util.FindViewUtil;
import com.aries.ui.widget.i.DialogInterface;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.IDialog;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: AriesHoo on 2018/7/19 10:46
 * @E-Mail: AriesHoo@126.com
 * Function: UIAlert Dialog+Builder模式重构
 * Description:
 * 1、2018-3-29 14:07:04 新增获取Title及Message回调
 * 2、新增控制Button点击是否关闭弹框属性{@link Builder#setButtonClickDismissEnable(boolean)}
 * 3、2019-9-16 13:29:33 修改{@link Builder#createButtons()}多个button 高度默认值避免高度适配异常
 */
public class UIAlertDialog extends BasisDialog<UIAlertDialog> {

    public UIAlertDialog(Context context) {
        super(context/*, R.style.AlertViewDialogStyle*/);
    }

    public interface ICreateButtons {

        Component createBeforeTitle();

        List<Component> createButtons();
    }

    /**
     * 获取Title
     *
     * @return 获取Title
     */
    public Text getTitle() {
        return FindViewUtil.getTargetView(mContentView, ResourceTable.Integer_tv_titleAlertDialog);
    }

    /**
     * 返回Message控件
     *
     * @return 获取Title
     */
    public Text getMessage() {
        return FindViewUtil.getTargetView(mContentView, ResourceTable.Integer_tv_messageAlertDialog);
    }

    /**
     * 获取Button
     * {@link DialogInterface#BUTTON_NEGATIVE}
     * {@link DialogInterface#BUTTON_NEUTRAL}
     * {@link DialogInterface#BUTTON_POSITIVE}
     *
     * @param witch witch
     * @return Button
     */
    public Button getButton(int witch) {
        int temp = witch == DialogInterface.BUTTON_NEUTRAL ?
                ResourceTable.Integer_btn_neutralAlertDialog : ResourceTable.Integer_btn_positiveAlertDialog;
        return FindViewUtil.getTargetView(mContentView, witch == DialogInterface.BUTTON_NEGATIVE ?
                ResourceTable.Integer_btn_negativeAlertDialog : temp);
    }

    /**
     * QQ风格--title上边一个横线
     */
    public static class DividerQQBuilder extends DividerBuilder<DividerQQBuilder> {

        protected Element mTitleDivider;
        protected int mTitleDividerHeight;

        public DividerQQBuilder(Context context) {
            super(context);
            setTitleDividerHeight((int) dp2px(4))
                    .setTitleDividerColor(Color.argb(255, 17, 183, 245))
                    .setBackgroundColor(Color.WHITE.getValue())
                    .setBorderLessButtonEnable(true)
                    .setBackgroundPressedColor(Color.argb(255, 245, 245, 245))
                    .setBackgroundRadius(6f)
                    .setPadding((int) dp2px(20))
                    .setNegativeButtonTextSize(18)
                    .setPositiveButtonTextSize(18)
                    .setNeutralButtonTextSize(18)
                    .setDividerColor(Color.argb(255, 230, 230, 230))
                    .setMessageTextColor(Color.BLACK)
                    .setNegativeButtonTextColor(Color.BLACK)
                    .setNeutralButtonTextColor(Color.BLACK)
                    .setPositiveButtonTextColor(Color.BLACK)
                    .setTitleTextColor(Color.BLACK)
                    .setTitleTextSize(20);
        }

        /**
         * 设置分割线drawable
         *
         * @param drawable
         * @return DividerQQBuilder
         */
        public DividerQQBuilder setTitleDivider(Element drawable) {
            mTitleDivider = drawable;
            return this;
        }

        public DividerQQBuilder setTitleDividerColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setTitleDivider(shapeElement);
        }

        public DividerQQBuilder setTitleDividerResource(int res) {
            return setTitleDivider((ShapeElement) mResourceUtil.getColorDrawable(res));
        }

        /**
         * 设置分割线宽度
         *
         * @param h
         * @return DividerQQBuilder
         */
        public DividerQQBuilder setTitleDividerHeight(int h) {
            mTitleDividerHeight = h;
            return this;
        }

        public DividerQQBuilder setTitleDividerHeightResource(int res) {
            return setTitleDividerHeight(mResourceUtil.getDimensionPixelSize(res));
        }

        protected Element getTitleDrawable(Element drawable) {
            if (drawable != null && drawable instanceof ShapeElement) {
                float[] radiusArr = new float[8];
                radiusArr[0] = mBackgroundRadius;
                radiusArr[1] = mBackgroundRadius;
                radiusArr[2] = mBackgroundRadius;
                radiusArr[3] = mBackgroundRadius;
                ((ShapeElement) drawable).setCornerRadiiArray(radiusArr);
            }
            return drawable;
        }

        @Override
        public Component createBeforeTitle() {
            if (mTitleDivider != null && mTitleDividerHeight > 0) {
                return getDivider(ComponentContainer.LayoutConfig.MATCH_PARENT,
                        mTitleDividerHeight,
                        getTitleDrawable(mTitleDivider));
            }
            return super.createBeforeTitle();
        }
    }

    public static class DividerIOSBuilder extends DividerBuilder<DividerIOSBuilder> {

        public DividerIOSBuilder(Context context) {
            super(context);
        }
    }

    /**
     * iOS风格
     */
    protected static class DividerBuilder<T extends DividerBuilder> extends Builder<T> {
        private ShapeElement mDivider;
        private int mDividerWidth;
        protected DirectionalLayout mLLayoutButton;
        protected List<Button> mListButton = new ArrayList<>();
        private int padding = (int) dp2px(10);
        private int minHeight = (int) dp2px(45);

        public DividerBuilder(Context context) {
            super(context);
            setDividerResource(ResourceTable.Color_colorAlertLineGray)
                    .setDividerWidthResource(/*dp2px(0.5f)*/ResourceTable.String_dp_line_size)
                    .setTitleTextColorResource(ResourceTable.Color_colorAlertTitle)
                    .setMessageTextColorResource(ResourceTable.Color_colorAlertMessage)
                    .setNegativeButtonTextColorResource(ResourceTable.Color_colorAlertButton)
                    .setNeutralButtonTextColorResource(ResourceTable.Color_colorAlertButton)
                    .setPositiveButtonTextColorResource(ResourceTable.Color_colorAlertButton)
                    .setBackgroundRadiusResource(/*dp2px(12f)*/ResourceTable.String_alert_radius);
        }

        /**
         * 设置分割线drawable
         *
         * @param drawable 分割线
         * @return backBuilder
         */
        public T setDivider(ShapeElement drawable) {
            mDivider = drawable;
            return backBuilder();
        }

        public T setDividerColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setDivider(shapeElement);
        }

        public T setDividerResource(int res) {
            return setDivider((ShapeElement) mResourceUtil.getColorDrawable(res));
        }

        /**
         * 设置分割线宽度
         *
         * @param w
         * @return backBuilder
         */
        public T setDividerWidth(int w) {
            mDividerWidth = w;
            return backBuilder();
        }

        public T setDividerWidthResource(int res) {
            return setDividerWidth(mResourceUtil.getDimensionPixelSize(res));
        }

        /**
         * 获取 分割线
         *
         * @param w
         * @param h
         * @param drawable
         * @return view
         */
        protected Component getDivider(int w, int h, Element drawable) {
            final Component view = new Component(mContext);
            view.setLayoutConfig(new ComponentContainer.LayoutConfig(w, h));
            setViewBackground(view, drawable);
            return view;
        }

        /**
         * 获取Button背景--圆角及按下效果转换
         *
         * @param gravity 效果转换
         * @return StateElement
         */
        protected StateElement getButtonDrawable(int gravity) {
            StateElement listDrawable = new StateElement();
            Element drawable = DrawableUtil.getNewDrawable(mBackground);
            Element drawablePressed = DrawableUtil.getNewDrawable(mBackgroundPressed);
            //圆角且 背景均为ColorDrawable才进行圆角操作非中间
            if (mBackgroundRadius > 0
                    && mBackground instanceof ShapeElement
                    && mBackgroundPressed != null) {
                ShapeElement gradient = new ShapeElement();
                ShapeElement gradientPressed = new ShapeElement();
                float[] radiusArr = new float[8];
                if (gravity == TextAlignment.LEFT || gravity == TextAlignment.BOTTOM) {//左边第一个/或只有一个
                    radiusArr[6] = mBackgroundRadius;
                    radiusArr[7] = mBackgroundRadius;
                }
                if (gravity == TextAlignment.RIGHT || gravity == TextAlignment.BOTTOM) {//右边/或只有一个
                    radiusArr[4] = mBackgroundRadius;
                    radiusArr[5] = mBackgroundRadius;
                }
                gradient.setCornerRadiiArray(radiusArr);
                if (drawable instanceof ShapeElement) {
                    gradient.setRgbColor(((ShapeElement) drawable).getRgbColors()[0]);
                }
//                else if (drawable instanceof GradientDrawable) {
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                        gradient.setCornerRadiiArray(((GradientDrawable) drawable).getColor().getDefaultColor());
//                    }
//                }
                gradientPressed.setCornerRadiiArray(radiusArr);
                gradientPressed.setRgbColor(((ShapeElement) drawablePressed).getRgbColors()[0]);

                drawable = gradient;
                drawablePressed = gradientPressed;
            }
            listDrawable.addState(new int[] {mStatePressed}, drawablePressed);
            listDrawable.addState(new int[] {}, drawable);
            return listDrawable;
        }

        /**
         * 添加Button
         *
         * @param sequence sequence
         * @param textSize textSize
         * @param textColor textColor
         * @param fakeBoldText fakeBoldText
         * @param witch witch
         * @param listener listener
         */
        protected void addButton(String sequence, int textSize,
                                 Color textColor, boolean fakeBoldText, final int witch, final IDialog.ClickedListener listener) {
            if (sequence == null || sequence.length() == 0) {
                return;
            }
            Button btn = new Button(mContext);
            if (mBorderLessButtonEnable) {
                btn = (Button) LayoutScatter.getInstance(mContext)
                        .parse(ResourceTable.Layout_layout_alert_button_border_less, null, true);
            }
            int temp = witch == DialogInterface.BUTTON_NEUTRAL ?
                    ResourceTable.Integer_btn_neutralAlertDialog : ResourceTable.Integer_btn_positiveAlertDialog;
            btn.setId(witch == DialogInterface.BUTTON_NEGATIVE ? ResourceTable.Integer_btn_negativeAlertDialog : temp);

            setTextAttribute(btn, sequence, textColor, textSize, TextAlignment.CENTER, fakeBoldText);
            setTextViewLine(btn);
            btn.setPadding(padding, padding, padding, padding);
            btn.setMinHeight(minHeight);
            btn.setMultipleLine(false);
//            btn.setEllipsize(TextUtils.TruncateAt.MIDDLE);
            btn.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (listener != null) {
                        listener.onClick(mDialog, witch);
                    }
                    if (mButtonClickDismissEnable) {
                        mDialog.hide();
                    }
                }
            });
            mListButton.add(btn);
        }

        @Override
        public Component createBeforeTitle() {
            return null;
        }

        @Override
        public List<Component> createButtons() {
            List<Component> listView = new ArrayList<>();
            addButton(mNegativeButtonStr, mNegativeButtonTextSize,
                    mNegativeButtonTextColor, mNegativeButtonTextFakeBoldEnable,
                    DialogInterface.BUTTON_NEGATIVE, mOnNegativeButtonClickListener);
            addButton(mNeutralButtonStr, mNeutralButtonTextSize,
                    mNeutralButtonTextColor, mNeutralButtonTextFakeBoldEnable,
                    DialogInterface.BUTTON_NEUTRAL, mOnNeutralButtonClickListener);
            addButton(mPositiveButtonStr, mPositiveButtonTextSize,
                    mPositiveButtonTextColor, mPositiveButtonTextFakeBoldEnable,
                    DialogInterface.BUTTON_POSITIVE, mOnPositiveButtonClickListener);
            if (mListButton.size() == 0) {
                return listView;
            }
            //画水平分割线
            listView.add(getDivider(ComponentContainer.LayoutConfig.MATCH_PARENT, mDividerWidth, mDivider));
            ComponentContainer.LayoutConfig params = new ComponentContainer.LayoutConfig(
                    ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
            if (mListButton.size() == 1) {
                Button btn = mListButton.get(0);
                btn.setLayoutConfig(params);
                setViewBackground(btn, getButtonDrawable(TextAlignment.BOTTOM));
                listView.add(btn);
                return listView;
            }
            //超过1个即水平LinearLayout包裹
            mLLayoutButton = new DirectionalLayout(mContext);
            mLLayoutButton.setMinHeight(minHeight);
            mLLayoutButton.setOrientation(Component.HORIZONTAL);
            mLLayoutButton.setLayoutConfig(params);
            int size = mListButton.size();

            for (int i = 0; i < size; i++) {
                Button btn = mListButton.get(i);
                btn.setLayoutConfig(new DirectionalLayout.LayoutConfig(
                        0, ComponentContainer.LayoutConfig.MATCH_CONTENT, DirectionalLayout.HORIZONTAL, 1.0f));
                mLLayoutButton.addComponent(btn);
                if (i != mListButton.size() - 1) {
                    //设置竖直分割线
                    mLLayoutButton.addComponent(getDivider(mDividerWidth, ComponentContainer.LayoutConfig.MATCH_PARENT, mDivider));
                }
                StateElement drawable;
                if (size == 2) {
                    drawable = getButtonDrawable(i == 0 ? TextAlignment.LEFT : TextAlignment.RIGHT);
                } else {
                    int temp = i == 1 ? TextAlignment.CENTER : TextAlignment.RIGHT;
                    drawable = getButtonDrawable(i == 0 ? TextAlignment.LEFT : temp);
                }
                setViewBackground(btn, drawable);
            }
            listView.add(mLLayoutButton);
            return listView;
        }
    }

    /**
     * 通用属性
     *
     * @param <T>
     */
    private static abstract class Builder<T extends Builder> extends BasisBuilder<T> implements ICreateButtons {

        protected boolean mBorderLessButtonEnable = true;
        protected Text mTvTitle;
        protected String mTitleStr;
        protected Color mTitleTextColor;
        protected int mTitleTextSize = 16;
        protected int mDialogmargin = 16;
        protected boolean mTitleTextFakeBoldEnable;
        protected int mTitleTextGravity = TextAlignment.CENTER;

        protected Text mTvMessage;
        protected String mMessageStr;
        protected Color mMessageTextColor;
        protected int mMessageTextSize = 16;
        protected boolean mMessageTextFakeBoldEnable;
        protected int mMessageTextGravity = TextAlignment.CENTER;
        protected int mCenterGravity = LayoutAlignment.CENTER;

        protected ShapeElement mBackgroundPressed;
        private List<Component> mListViews = new ArrayList<>();

        protected DirectionalLayout mLLayoutContainer;
        protected ScrollView mSvView;
        protected DirectionalLayout mLLayoutView;

        protected boolean mButtonClickDismissEnable = true;
        protected String mNegativeButtonStr;
        protected Color mNegativeButtonTextColor;
        protected int mNegativeButtonTextSize = 16;
        protected boolean mNegativeButtonTextFakeBoldEnable;
        protected IDialog.ClickedListener mOnNegativeButtonClickListener;

        protected String mNeutralButtonStr;
        protected Color mNeutralButtonTextColor;
        protected int mNeutralButtonTextSize = 16;
        protected boolean mNeutralButtonTextFakeBoldEnable;
        protected IDialog.ClickedListener mOnNeutralButtonClickListener;

        protected String mPositiveButtonStr;
        protected Color mPositiveButtonTextColor;
        protected int mPositiveButtonTextSize = 16;
        protected boolean mPositiveButtonTextFakeBoldEnable;
        protected IDialog.ClickedListener mOnPositiveButtonClickListener;

        public Builder(Context context) {
            super(context);
            setBackgroundResource(ResourceTable.Color_colorAlertBg)
                    .setBackgroundPressedResource(ResourceTable.Color_colorAlertBgPressed)
                    .setTitleTextColorResource(ResourceTable.Color_colorAlertTitle)
                    .setMinWidthResource(ResourceTable.String_alert_min_width)
                    .setMinHeightResource(ResourceTable.String_alert_min_height)
                    .setPadding(mResourceUtil.getDimensionPixelSize(ResourceTable.String_alert_padding));
        }

        /**
         * 设置5.0以后Button 按下无阴影
         *
         * @param enable 按下无阴影
         * @return backBuilder
         */
        public T setBorderLessButtonEnable(boolean enable) {
            this.mBorderLessButtonEnable = enable;
            return backBuilder();
        }

        /**
         * 设置Button按下背景Drawable
         *
         * @param drawable Drawable
         * @return backBuilder
         */
        public T setBackgroundPressed(ShapeElement drawable) {
            this.mBackgroundPressed = drawable;
            return backBuilder();
        }

        /**
         * 设置根布局背景颜色值
         *
         * @param color 背景颜色值
         * @return 根布局背景
         */
        public T setBackgroundPressedColor(int color) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
            return setBackground(shapeElement);
//            return setBackgroundPressed(new ShapeElement(mContext, color));
        }

        /**
         * 设置根布局背景资源
         *
         * @param resId 背景资源
         * @return 设置根布局
         */
        public T setBackgroundPressedResource(int resId) {
            return setBackgroundPressed((ShapeElement) mResourceUtil.getColorDrawable(resId));
        }

        /**
         * 设置标题
         * {@link Text#setText(String)}
         *
         * @param charSequence charSequence
         * @return backBuilder
         */
        public T setTitle(String charSequence) {
            this.mTitleStr = charSequence;
            return backBuilder();
        }

        public T setTitle(int resId) {
            return setTitle(mResourceUtil.getText(resId));
        }

        /**
         * 设置标题文字颜色
         * {@link Text#setTextColor(Color)}
         *
         * @param color 文字颜色
         * @return backBuilder
         */
        public T setTitleTextColor(Color color) {
            this.mTitleTextColor = color;
            return backBuilder();
        }

        public T setTitleTextColor(int color) {
            return setTitleTextColor(new Color(color));
        }

        public T setTitleTextColorResource(int resId) {
            return setTitleTextColor(mResourceUtil.getColor(resId));
        }

        /**
         * 设置标题文本尺寸
         * {@link Text#setTextSize(int, Text.TextSizeType)}
         * {@link #setTextSizeUnit(Text.TextSizeType)}
         *
         * @param size 文本尺寸
         * @return backBuilder
         */
        public T setTitleTextSize(int size) {
            this.mTitleTextSize = size;
            return backBuilder();
        }

        /**
         * 设置文本是否粗体
         *
         * @param enable 是否粗体
         * @return backBuilder
         */
        public T setTitleTextFakeBoldEnable(boolean enable) {
            this.mTitleTextFakeBoldEnable = enable;
            return backBuilder();
        }

        /**
         * 设置标题文本对齐方式
         * {@link Text#setTextAlignment(int)}
         *
         * @param gravity 对齐方式
         * @return backBuilder
         */
        public T setTitleTextGravity(int gravity) {
            this.mTitleTextGravity = gravity;
            return backBuilder();
        }

        /**
         * 设置标题
         * {@link Text#setText(String)}
         *
         * @param charSequence charSequence
         * @return backBuilder
         */
        public T setMessage(String charSequence) {
            this.mMessageStr = charSequence;
            return backBuilder();
        }

        public T setMessage(int resId) {
            return setMessage(mResourceUtil.getText(resId));
        }

        /**
         * 设置标题文字颜色
         * {@link Text#setTextColor(Color)}
         *
         * @param color 文字颜色
         * @return backBuilder
         */
        public T setMessageTextColor(Color color) {
            this.mMessageTextColor = color;
            return backBuilder();
        }

        public T setMessageTextColor(int color) {
            return setMessageTextColor(new Color(color));
        }

        public T setMessageTextColorResource(int resId) {
            return setMessageTextColor(mResourceUtil.getColor(resId));
        }

        /**
         * 设置标题文本尺寸
         * {@link Text#setTextSize(int, Text.TextSizeType)}
         * {@link #setTextSizeUnit(Text.TextSizeType)}
         *
         * @param size 文本尺寸
         * @return 文本尺寸
         */
        public T setMessageTextSize(int size) {
            this.mMessageTextSize = size;
            return backBuilder();
        }

        /**
         * 设置文本是否粗体
         *
         * @param enable enable
         * @return 是否粗体
         */
        public T setMessageTextFakeBoldEnable(boolean enable) {
            this.mMessageTextFakeBoldEnable = enable;
            return backBuilder();
        }

        /**
         * 设置标题文本对齐方式
         * {@link Text#setTextAlignment(int)}
         *
         * @param gravity 对齐方式
         * @return backBuilder
         */
        public T setMessageTextGravity(int gravity) {
            this.mMessageTextGravity = gravity;
            return backBuilder();
        }

        /**
         * 设置 Message相应父容器对齐方式
         *
         * @param gravity 齐方式
         * @return backBuilder
         */
        public T setCenterGravity(int gravity) {
            this.mCenterGravity = gravity;
            return backBuilder();
        }

        /**
         * 添加View
         *
         * @param v
         * @return backBuilder
         */
        public T setView(Component v) {
            if (v != null) {
                mListViews.add(v);
            }
            return backBuilder();
        }

        public T setView(int res) {
            return setView(LayoutScatter.getInstance(mContext).parse(res, null, true));
        }

        /**
         * 设置点击Button是否关闭弹窗
         *
         * @param enable n是否关闭弹窗
         * @return backBuilder
         */
        public T setButtonClickDismissEnable(boolean enable) {
            this.mButtonClickDismissEnable = enable;
            return backBuilder();
        }

        /**
         * 设置Button
         * {@link Text#setText(String)}
         *
         * @param charSequence charSequence
         * @param listener listener
         * @return backBuilder
         */
        public T setNegativeButton(String charSequence, IDialog.ClickedListener listener) {
            this.mNegativeButtonStr = charSequence;
            this.mOnNegativeButtonClickListener = listener;
            return backBuilder();
        }

        public T setNegativeButton(int resId, IDialog.ClickedListener listener) {
            return setNegativeButton(mResourceUtil.getText(resId), listener);
        }

        /**
         * 设置文字颜色
         * {@link Text#setTextColor(Color)}
         *
         * @param color 文字颜色
         * @return backBuilder
         */
        public T setNegativeButtonTextColor(Color color) {
            this.mNegativeButtonTextColor = color;
            return backBuilder();
        }

        public T setNegativeButtonTextColor(int color) {
            return setNegativeButtonTextColor(new Color(color));
        }

        public T setNegativeButtonTextColorResource(int resId) {
            return setNegativeButtonTextColor(mResourceUtil.getColor(resId));
        }

        /**
         * 设置文本尺寸
         * {@link Text#setTextSize(int, Text.TextSizeType)}
         * {@link #setTextSizeUnit(Text.TextSizeType)}
         *
         * @param size 文本尺寸
         * @return backBuilder
         */
        public T setNegativeButtonTextSize(int size) {
            this.mNegativeButtonTextSize = size;
            return backBuilder();
        }

        /**
         * 设置文本是否粗体
         *
         * @param enable 是否粗体
         * @return backBuilder
         */
        public T setNegativeButtonFakeBoldEnable(boolean enable) {
            this.mNegativeButtonTextFakeBoldEnable = enable;
            return backBuilder();
        }

        /**
         * 设置Button
         * {@link Text#setText(String)}
         *
         * @param listener listener
         * @param charSequence charSequence
         * @return backBuilder
         */
        public T setNeutralButton(String charSequence, IDialog.ClickedListener listener) {
            this.mNeutralButtonStr = charSequence;
            this.mOnNeutralButtonClickListener = listener;
            return backBuilder();
        }

        public T setNeutralButton(int resId, IDialog.ClickedListener listener) {
            return setNeutralButton(mResourceUtil.getText(resId), listener);
        }

        /**
         * 设置文字颜色
         * {@link Text#setTextSize(int, Text.TextSizeType)}
         *
         * @param color 文字颜色
         * @return backBuilder
         */
        public T setNeutralButtonTextColor(Color color) {
            this.mNeutralButtonTextColor = color;
            return backBuilder();
        }

        public T setNeutralButtonTextColor(int color) {
            return setNeutralButtonTextColor(new Color(color));
        }

        public T setNeutralButtonTextColorResource(int resId) {
            return setNeutralButtonTextColor(mResourceUtil.getColor(resId));
        }

        /**
         * 设置文本尺寸
         * {@link Text#setTextSize(int, Text.TextSizeType)}
         * {@link ##setTextSizeUnit(Text.TextSizeType)}
         *
         * @param size  size
         * @return  backBuilder
         */
        public T setNeutralButtonTextSize(int size) {
            this.mNeutralButtonTextSize = size;
            return backBuilder();
        }

        /**
         * 设置文本是否粗体
         *
         * @param enable 是否粗体
         * @return backBuilder
         */
        public T setNeutralButtonFakeBoldEnable(boolean enable) {
            this.mNeutralButtonTextFakeBoldEnable = enable;
            return backBuilder();
        }

        /**
         * 设置Button
         * {@link Text#setText(String)}
         *  @param listener  listener
         * @param charSequence charSequence
         * @return backBuilder
         */
        public T setPositiveButton(String charSequence, IDialog.ClickedListener listener) {
            this.mPositiveButtonStr = charSequence;
            this.mOnPositiveButtonClickListener = listener;
            return backBuilder();
        }

        public T setPositiveButton(int resId, IDialog.ClickedListener listener) {
            return setPositiveButton(mResourceUtil.getText(resId), listener);
        }

        /**
         * 设置文字颜色
         * {@link Text#setTextColor(Color)}
         *
         * @param color 文字颜色
         * @return backBuilder
         */
        public T setPositiveButtonTextColor(Color color) {
            this.mPositiveButtonTextColor = color;
            return backBuilder();
        }

        public T setPositiveButtonTextColor(int color) {
            return setPositiveButtonTextColor(new Color(color));
        }

        public T setPositiveButtonTextColorResource(int resId) {
            return setPositiveButtonTextColor(mResourceUtil.getColor(resId));
        }

        /**
         * 设置文本尺寸
         * {@link Text#setTextSize(int, Text.TextSizeType)}
         * {@link #setTextSizeUnit(Text.TextSizeType)}
         *
         * @param size 文本尺寸
         * @return backBuilder
         */
        public T setPositiveButtonTextSize(int size) {
            this.mPositiveButtonTextSize = size;
            return backBuilder();
        }

        /**
         * 设置文本是否粗体
         *
         * @param enable 是否粗体
         * @return backBuilder
         */
        public T setPositiveButtonFakeBoldEnable(boolean enable) {
            this.mPositiveButtonTextFakeBoldEnable = enable;
            return backBuilder();
        }

        public UIAlertDialog create() {
            int margin = (int) dp2px(mDialogmargin);
            Component contentView = createContentView();
            mDialog = new UIAlertDialog(mContext);
            mDialog.setContentView(contentView);
            setDialog();
            mDialog.setMargin(margin * 3, margin, margin * 3, margin);
            mDialog.setGravity(LayoutAlignment.CENTER);
            mDialog.setTransparent(true);
            return (UIAlertDialog) mDialog;
        }

        private Component createContentView() {
            mLLayoutRoot = new DirectionalLayout(mContext);
            mLLayoutRoot.setId(ResourceTable.Integer_lLayout_rootAlertDialog);
            mLLayoutRoot.setLayoutConfig(new DependentLayout.LayoutConfig((int) (getScreenWidth() - dp2px(mDialogmargin * 6)), DependentLayout.LayoutConfig.MATCH_CONTENT));
            mLLayoutRoot.setOrientation(DirectionalLayout.VERTICAL);
            mLLayoutRoot.setMinWidth(mMinWidth);
            mLLayoutRoot.setMinHeight(mMinHeight);
            setRootView();
            if (createBeforeTitle() != null) {
                mLLayoutRoot.addComponent(createBeforeTitle());
            }
            createTitle();
            createMessage();
            createContainerView();
            mLLayoutRoot.setPadding(0, 0, 0, 0);
            for (Component v : createButtons()) {
                if (v != null) {
                    mLLayoutRoot.addComponent(v);
                }
            }
            return mLLayoutRoot;
        }

        private void createTitle() {
            if (mTitleStr == null || mTitleStr.length() == 0) {
                return;
            }
            mTvTitle = new Text(mContext);
            mTvTitle.setLayoutConfig(new ComponentContainer.LayoutConfig(
                    ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
            mTvTitle.setMaxTextLines(2);
            mTvTitle.setId(ResourceTable.Integer_tv_titleAlertDialog);
            mLLayoutRoot.addComponent(mTvTitle);

            setTextViewLine(mTvTitle);
            setTextAttribute(mTvTitle, mTitleStr, mTitleTextColor, mTitleTextSize, mTitleTextGravity, mTitleTextFakeBoldEnable);
            mTvTitle.setPadding(mPadding, mPadding, mPadding, 0);
        }

        private void createContainerView() {
            mLLayoutContainer = new DirectionalLayout(mContext);
            mLLayoutContainer.setId(ResourceTable.Integer_lLayout_containerAlertDialog);
            mLLayoutContainer.setLayoutConfig(new DirectionalLayout.LayoutConfig(
                    DirectionalLayout.LayoutConfig.MATCH_PARENT, 0, DirectionalLayout.VERTICAL, 1.0f));
            mLLayoutContainer.setOrientation(DirectionalLayout.VERTICAL);
            mLLayoutContainer.setPadding(mPadding, 0/*dp2px(12)*/, mPadding, 0/*dp2px(12)*/);
            mLLayoutContainer.setMarginTop((int) dp2px(12));
            mLLayoutContainer.setMarginBottom((int) dp2px(12));

            mLLayoutContainer.setAlignment(mCenterGravity);

            mLLayoutRoot.addComponent(mLLayoutContainer);

            mLLayoutView = new DirectionalLayout(mContext);
            mLLayoutContainer.setId(ResourceTable.Integer_lLayout_ViewAlertDialog);
            mLLayoutView.setOrientation(DirectionalLayout.VERTICAL);
            mLLayoutView.setLayoutConfig(new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));

            mSvView = new ScrollView(mContext);
            mSvView.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
//            mSvView.setOverScrollMode(Component.OVER_SCROLL_NEVER);
            mSvView.setVibrationEffectEnabled(false);
            mSvView.addComponent(mLLayoutView);

            mLLayoutContainer.addComponent(mSvView);
            if (mListViews != null) {
                for (Component v : mListViews) {
                    mLLayoutView.addComponent(v);
                }
            }

        }

        private void createMessage() {
            if (mMessageStr == null || mMessageStr.length() == 0) {
                return;
            }
            ComponentContainer.LayoutConfig params = new ComponentContainer.LayoutConfig(
                    ComponentContainer.LayoutConfig.MATCH_PARENT,
                    ComponentContainer.LayoutConfig.MATCH_CONTENT);
            mTvMessage = new Text(mContext);
            mTvMessage.setLayoutConfig(params);
            mTvMessage.setMultipleLine(true);
            mTvMessage.setId(ResourceTable.Integer_tv_messageAlertDialog);
            mListViews.add(0, mTvMessage);

            setTextAttribute(mTvMessage, mMessageStr, mMessageTextColor, mMessageTextSize, mMessageTextGravity, mMessageTextFakeBoldEnable);
            new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                @Override
                public void run() {
                    int lineCount = mTvMessage.getMaxTextLines();
                    if (lineCount > 1) {
//                        mTvMessage.setTextAlignment(TextAlignment.LEFT | mTvMessage.getTextAlignment());
                        mTvMessage.setTextAlignment(TextAlignment.LEFT);
                    }
                    if (mOnTextViewLineListener != null) {
                        mOnTextViewLineListener.onTextViewLineListener(mTvMessage, lineCount);
                    }
                }
            });
        }
    }
}

